package org.youyun56.gpsserver.command;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.youyun56.gpsserver.Task;
import org.youyun56.gpsserver.common.StringUtils;
import org.youyun56.gpsserver.common.YouyunMap;
import org.youyun56.gpsserver.entity.Im;

public class AddImExecutor extends Task {
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public Object execute() throws Exception {
        YouyunMap map = command.getParameters();
        String txt = map.get("txt");
        String mobiles = map.get("mobiles");
        String citys = map.get("citys");
        Long userId = map.getLong("userId");
        String attach = map.get("attach");
        List<Im> ims = new ArrayList<Im>();
        if (StringUtils.isNotEmpty(txt, mobiles, userId, attach, citys)) {
            attach = URLDecoder.decode(attach, UTF8);
            txt = URLDecoder.decode(txt, UTF8);
            String[] mobileSpil = mobiles.split(",");
            String[] citySpil = citys.split(",");
            String addDay = sdf.format(new Date());
            for (int i = 0; i < mobileSpil.length; i++) {
                Im im = new Im();
                im.setAddDay(addDay);
                im.setCityId(Integer.parseInt(citySpil[i]));
                im.setContext(txt);
                im.setMobile(mobileSpil[i]);
                im.setUserId(userId);
                im.setAttach(attach);
                ims.add(im);
            }
        }
        return ims;
    }
}
